package forList;

import java.util.ArrayList;

/**
 * @author hanj.cn@outlook.com
 */
public class EnsureCapacityTest {

    public static void main(String[] args) {

        withEnsureCapacity(); //2829;
        withoutEnsureCapacity(); //3144;
    }

    public static void withoutEnsureCapacity() {

        ArrayList<Object> list = new ArrayList<>();
        final int N = 10000000;
        list = new ArrayList<Object>();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {
            list.add(i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("不使用ensureCapacity方法耗时:" + (endTime - startTime));
    }

    public static void withEnsureCapacity() {

        ArrayList<Object> list = new ArrayList<>();
        final int N = 10000000;
        list = new ArrayList<Object>();
        long startTime = System.currentTimeMillis();
        list.ensureCapacity(N);
        for (int i = 0; i < N; i++) {
            list.add(i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("使用ensureCapacity方法耗时:" + (endTime - startTime));
    }

}
